<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Charge_m extends FOX_Model
{
    function __construct()
    {
        parent::__construct();
    }
    public function add_charge($data){
        $this->db->insert('charge',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return FALSE;
        }

    }
    public function get_one_charge($charge_id){
        $this->db->select('*');
        $this->db->from('charge');
        $this->db->where('charge_id',$charge_id);
        $this->db->where('delete',0);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }else{

        }
    }
    public function update_charge($charge_id,$data){
        $this->db->where('charge_id',$charge_id);
        $this->db->update('charge', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    public function count_charge_find($key){
        $this->db->select('charge_id');
        $this->db->from('charge');
        $this->db->join('project','project.project_id = charge.project_id','left');
        if($key){
            $this->db->like('project.project_name', $key);
        }
        $this->db->where('charge.delete',0);
        $total=$this->db->count_all_results();
        return $total;
    }
    public function get_charge_list($limit,$start,$key){
        $this->db->select('charge.*,project.project_id,project.project_name');
        $this->db->from('charge');
        $this->db->join('project','project.project_id = charge.project_id','left');
        $this->db->limit($start,$limit);
        if($key){
            $this->db->like('project.project_name', $key);
        }
        $this->db->where('charge.delete',0);
        $this->db->order_by('project.project_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    public function get_charge_by_project_id($project_id){

    }
    public function count_project_charge(){
        $this->db->select_sum('charge_money');
        $this->db->from('charge');
        $this->db->where('delete',0);
        $query = $this->db->get();
        $result = $query->result();
        return ($result[0]->charge_money)?$result[0]->charge_money:'0.00';
    }
    public function update_project_charge($project_id,$data){
        $this->db->where('project_id',$project_id);
        $this->db->update('charge', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

}
